<template>
  <div>
    <h2>评价列表</h2>
    <ul>
      <li v-for="review in reviews" :key="review.reviewId">
        用户: {{ review.userId }}，评分: {{ review.rating }}，评论: {{ review.comment }}
        <div>
          回复：
          <textarea v-model="review.reply"></textarea>
          <button @click="replyReview(review.reviewId, review.reply)">回复</button>
        </div>
      </li>
    </ul>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

const reviews = ref([])

async function fetchReviews() {
  const res = await axios.get('/api/v1/admin/reviews', {
    headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
    params: { page: 1, pageSize: 20 }
  })
  if (res.data.code === 0) {
    reviews.value = res.data.data.reviews
  }
}

async function replyReview(reviewId, reply) {
  const res = await axios.post(`/api/v1/admin/reviews/${reviewId}/reply`, { reply }, {
    headers: { Authorization: `Bearer ${localStorage.getItem('token')}` }
  })
  if (res.data.code === 0) {
    alert('回复成功')
    fetchReviews()
  } else {
    alert(res.data.message || '回复失败')
  }
}

onMounted(fetchReviews)
</script>
